$.ajaxSetup({
	cache : false
});
$.get(getRootPath() + '/customerajax/securityCenter!queryAccountInfo.action', function(data) {
	var level = 'low';
	var levelInfo = '低';
	switch (data.bindNum) {
	case 1:
		level = 'medium';
		levelInfo = '中';
		break;
	case 2:
		levelInfo = '高';
		level = 'high';
		break;
	}
	$('.safety-level .level:eq(' + data.bindNum + ')').prevAll().addClass(level);
	$('.safety-level .level:eq(' + data.bindNum + ')').addClass(level);
	$('.safety-level .level_info').text(levelInfo);
});

function getPaymentType(type) {
	switch (type) {
	case "1":
	case 1:
		return "在线交易";
	default:
		return "餐到付款";
	}
}

function getTimeStr(time) {
	var date = new Date(time);
	var dateStr = date.getFullYear();
	dateStr += '-';
	dateStr += date.getMonth() > 8 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1);
	dateStr += '-';
	dateStr += date.getDate() > 8 ? date.getDate() : '0' + date.getDate();
	dateStr += ' ';
	dateStr += date.getHours() > 8 ? date.getHours() : '0' + date.getHours();
	dateStr += ':';
	dateStr += date.getMinutes() > 8 ? date.getMinutes() : '0' + date.getMinutes();
	return dateStr;
}

function getOrderStatus(status) {
	switch (status) {
	case 1:
	case 7:
	case 12:
		return '下单成功';
	case 3:
	case 8:
		return '已确认';
	case 4:
		return '已派送';
	case 6:
	case 11:
		return '订单取消';
	case 9:
		return '等待付款';
	case 10:
		return '订单失败';
	case 13:
		return '订单完成';
	case 2:
	case 5:
	default:
		return '订单异常';
	}
}

function getOnlineOrderStatus(type, status, rechargeType, id) {
	// row.type == 2 ? '充值' : '飞饭外卖'
	if (type == 2) {
		switch (status) {
		case 1:
			if (rechargeType == 1) {
				return '进行中 | <span class="toRecharge" id="' + id + '">完成充值</span>';
			} else {
				return '进行中';
			}
		case 2:
			return '成功';
		default:
			return '失败';
		}
	} else {
		switch (status) {
		case 9:
			return '进行中';
		case 6:
		case 10:
			return '失败';
		default:
			return '成功';
		}
	}
}

function getPager(type, total, pageNum) {
	var pageNums = Math.ceil(total / 10);
	$('.' + type + ' .pager').empty();
	var pagerStr = "";
	if (pageNum > 1) {
		pagerStr += '<a class="uppage" page="' + (pageNum - 1) + '">上一页</a>';
	} else {
		pagerStr += '<span class="uppage">上一页</span>';
	}
	if(pageNums > 5 && pageNum>3){
		pagerStr += '<a page="1"> 1 </a>';
		pagerStr += '<b> ... </b>';
	}
	for (var i = 1; i <= pageNums; i++) {
		if (pageNums > 5) {
			if(i - 3 <= pageNum && i + 3 > pageNum){
				if (pageNum != i) {
					pagerStr += '<a page="' + i + '"> ' + i + ' </a>';
				} else {
					pagerStr += '<b class="thispage"> ' + i + ' </b>';
				}
			}
		}else{
			if (pageNum != i) {
				pagerStr += '<a page="' + i + '"> ' + i + ' </a>';
			} else {
				pagerStr += '<b class="thispage"> ' + i + ' </b>';
			}
		}
	}
	if(pageNums > 5 && pageNum<pageNums-3){
		pagerStr += '<b> ... </b>';
		pagerStr += '<a page="'+pageNums+'"> '+pageNums+' </a>';
	}
	if (pageNum < pageNums) {
		pagerStr += '<a class="downpage" page="' + (Number(pageNum) + 1) + '">下一页</a>';
	} else {
		pagerStr += '<span class="downpage">下一页</span>';
	}
	$('.' + type + ' .pager').append(pagerStr);
	
	$('.pager a').on('click', function() {
		if ($(this).parents('.online').length > 0) {
			listOnline($(this).attr('page'));
		} else {
			listOrders($(this).attr('page'));
		}
	});
}

$('.pager a').on('click', function() {
	if ($(this).parents('.online').length > 0) {
		listOnline($(this).attr('page'));
	} else {
		listOrders($(this).attr('page'));
	}
});

function listOrders(pageNum) {
	$.getJSON(getRootPath() + '/mvc/order/ordersLately', {
		pageNum : pageNum,
		pageSize : 10
	}, function(data) {
		if (data.status == 'success') {
			if (data.data.list.length > 0) {
				$('.orderArea li:not(.th)').remove();
				var html = "";
				for ( var i in data.data.list) {
					var row = data.data.list[i];
					var paymentType = getPaymentType(row.paymentType);
					var timeCreated = getTimeStr(row.timeCreated);
					var orderStatus = getOrderStatus(row.orderStatus);
					html += "<li><div class=\"listItem li_0 uncomment\"></div>\n" + "<div class=\"listItem li_1\">"
							+ row.orderSequence + "</div>\n" + "<div class=\"listItem li_2\">" + timeCreated
							+ "</div>\n" + "<div class=\"listItem li_3\">" + paymentType + "</div>\n"
							+ "<div class=\"listItem li_4\">" + row.orderItem + "</div>\n"
							+ "<div class=\"listItem li_5\">￥" + row.totalAmount + "</div>\n"
							+ "<div class=\"listItem li_6\">" + orderStatus + "</div><div class=\"c_l\"></div></li>";
				}
				$('.orderArea .body').append(html);
				
				getPager('orderArea', data.data.total, pageNum);
				$('.orderArea .body li').show();
				$('.orderArea .pager').show();
				$('.orderArea .body .noOrders').hide();
				resetFooter_2();
			} else {
				$('.orderArea .body li').hide();
				$('.orderArea .pager').hide();
				$('.orderArea .body .noOrders').show();
			}
		} else {
			alert(data.msg);
		}
	});
}

function listOnline(pageNum) {
	$.getJSON(getRootPath() + '/mvc/order/onlineLately', {
		pageNum : pageNum,
		pageSize : 10
	}, function(data) {
		if (data.status == 'success') {
			if (data.data.list.length > 0) {
				$('.online li:not(.th)').remove();
				var html = "";
				for ( var i in data.data.list) {
					var row = data.data.list[i];
					var timeCreated = getTimeStr(row.timeCreated);
					var orderStatus = getOnlineOrderStatus(row.type, row.status, row.rechargeType, row.id);
					html += "<li><div class=\"listItem li_0 uncomment\"></div>\n" + "<div class=\"listItem li_1\">"
							+ timeCreated + "</div>\n" + "<div class=\"listItem li_2\">"
							+ (row.type == 1 ? '飞饭外卖' : row.rechargeType == 3 ? '退款' : '充值') + "</div>\n"
							+ "<div class=\"listItem li_3\">" + (row.detail ? row.detail : '') + "</div>\n"
							+ "<div class=\"listItem li_4\">" + (row.type == 2 ? '+' : '-') + "￥" + row.amount
							+ "</div>\n" + "<div class=\"listItem li_5\">" + orderStatus
							+ "</div><div class=\"c_l\"></div></li>";
				}
				$('.online .body').append(html);
				getPager('online', data.data.total, pageNum);
				$('.online .body li').show();
				$('.online .pager').show();
				$('.online .body .noOrders').hide();
			} else {
				$('.online .body li').hide();
				$('.online .pager').hide();
				$('.online .body .noOrders').show();
			}
		} else {
			alert(data.msg);
		}
	});
}

listOrders(1);
listOnline(1);
$('.subnav a').click(function() {
	var type = $(this).attr('val');
	$('.subnav a').removeClass('active');
	$(this).addClass('active');
	switch (type) {
	case "0":
		$('.online').hide();
		$('.orderArea').show();
		break;
	case "1":
		$('.orderArea').hide();
		$('.online').show();
		break;
	}
});

$('.toRecharge').on('click', function() {
	var id = $(this).attr('id');
	if (id) {
		window.open(getRootPath() + '/mvc/web/payment/recharge/retry?id=' + id);
	}
});

$('.infoClose').click(function() {
	$('.message-info').hide(1000);
});
